# Replication Archive for: Schwarz, Susanne and Alexander Coppock. (2020) 
# "What Have We Learned About Gender From Candidate Choice Experiments? 
# A Meta-analysis of 67 Factorial Survey Experiments" 
# Forthcoming, Journal of Politics.

rm(list = ls())
library(tidyverse)
library(reshape2)
library(estimatr)
library(rmeta)
library(janitor)

source("helpers.R")


# Figure 4 ----------------------------------------------------------------

resp_pid_cate_estimates <-
  read_rds("resp_pid_cate_estimates.rds")

summary_df <-
  resp_pid_cate_estimates %>%
  group_by(resp_pid_3) %>%
  do(tidy(with(
    ., meta.summaries(estimate, std.error, method = "random")
  )))

# are the differences-in-cates significant?
summary_df %>%
  melt(id.vars = c("resp_pid_3")) %>%
  dcast(. ~ resp_pid_3 + variable) %>%
  clean_names() %>%
  transmute(
    D_R_estimate = democratic_respondents_estimate - republican_respondents_estimate,
    D_R_std.error = sqrt(
      democratic_respondents_std_error ^ 2 + republican_respondents_std_error ^ 2
    ),
    D_R_statistic = D_R_estimate / D_R_std.error,
    D_R_p.value = 2 * pnorm(abs(D_R_statistic), lower.tail = FALSE),
    I_R_estimate = independent_respondents_estimate - republican_respondents_estimate,
    I_R_std.error = sqrt(
      independent_respondents_std_error ^ 2 + republican_respondents_std_error ^ 2
    ),
    I_R_statistic = I_R_estimate / I_R_std.error,
    I_R_p.value = 2 * pnorm(abs(D_R_statistic), lower.tail = FALSE)
  )

# study data --------------------------------------------------------------

summary_df <-
  summary_df %>%
  mutate(
    study_name_short = "Random-effects meta-analysis",
    estimate_100 = estimate * 100,
    std.error_100 = std.error * 100,
    conf.high_100 = conf.high * 100,
    conf.low_100 = conf.low * 100
  )

gg_df <-
  resp_pid_cate_estimates %>%
  bind_rows(summary_df) %>%
  mutate(
    facet = as.numeric(study_name_short == "Random-effects meta-analysis"),
    estimate_100 = estimate * 100,
    std.error_100 = std.error * 100,
    conf.high_100 = conf.high * 100,
    conf.low_100 = conf.low * 100,
    se_entry = make_se_entry(estimate_100, std.error_100, digits = 1)
  )


difference_df <-
  gg_df %>%
  filter(resp_pid_3 %in% c("Democratic respondents", "Republican respondents")) %>%
  select(study_name_short, resp_pid_3, estimate) %>%
  melt(id.vars = c("study_name_short", "resp_pid_3")) %>%
  dcast(study_name_short ~ resp_pid_3 + variable) %>%
  mutate(
    difference = `Democratic respondents_estimate` - `Republican respondents_estimate`,
    study_name_short_fac = fct_reorder(study_name_short, difference)
  )

gg_df <-
  gg_df %>%
  mutate(study_name_short = factor(
    study_name_short,
    levels = levels(difference_df$study_name_short_fac)
  ))


# All estimates
g <-
  ggplot(gg_df, aes(x = estimate_100, y = study_name_short)) +
  geom_point(alpha = 0.8, stroke = 0) +
  geom_errorbarh(aes(xmin = conf.low_100, xmax = conf.high_100),
                 height = 0,
                 alpha = 0.5) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_vline(data = summary_df, aes(xintercept = estimate_100)) +
  geom_rect(
    data = summary_df,
    aes(
      xmin = conf.low_100,
      xmax = conf.high_100,
      x = NULL,
      y = NULL
    ),
    ymin = 0,
    ymax = 100,
    alpha = .1
  ) +
  geom_text(
    x = 20,
    aes(y = study_name_short, label = se_entry),
    hjust = 1,
    size = 1.5,
    nudge_y = 0.2
  ) +
  coord_cartesian(xlim = c(-20, 20)) +
  xlab("CATE estimate in percentage points") +
  facet_grid(
    rows = vars(facet),
    cols = vars(resp_pid_3),
    scales = "free_y",
    space = "free"
  ) +
  theme_bw() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_text(size = 8),
    legend.position = "bottom",
    strip.background = element_blank(),
    strip.text.y = element_blank(),
    text = element_text(size = 8)
  )

g

# ggsave(
#   "meta_cate_resp_pid.pdf",
#   g,
#   height = 4,
#   width = 6.5
# )
